package vn.hanelsoft.newsduet.control;

import java.util.ArrayList;

import vn.hanelsoft.newsduet.R;
import vn.hanelsoft.newsduet.model.adapter.AdapterIndiaNews;
import vn.hanelsoft.newsduet.model.helper.DBHandler;
import vn.hanelsoft.newsduet.model.object.ObjectCategory;
import vn.hanelsoft.newsduet.model.object.ObjectNews;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.TextView;
import android.widget.ViewFlipper;
import eu.erikw.PullToRefreshListView;
import eu.erikw.PullToRefreshListView.OnRefreshListener;

@SuppressLint("ValidFragment")
public class FavouriteFragment extends Fragment {
	Context mContext;
	ViewGroup root;
	int LOADING = 0, HOME = 1, ERRO = 2;
	ViewFlipper vf;
	PullToRefreshListView home_list_item;
	int page;
	TextView txt_msg;
	AdapterIndiaNews adapter;

	private View mLoadMoreFooter;
	String fa_array = "";
	int totalPage = 0;
	final int DEFAULT = 1;

	ArrayList<ObjectCategory> arrayCategory = new ArrayList<ObjectCategory>();
	boolean is_loop = true;
	boolean is_refresh = false;

	public FavouriteFragment(Context mContext) {
		this.mContext = mContext;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = (ViewGroup) inflater.inflate(R.layout.fragment_home, null);
		initWidget();
		getDataService(DEFAULT);
		return root;
	}

	private void getDataService(int page) {
		this.page = page;
		getDBFavouriteTask task = new getDBFavouriteTask();
		task.execute();
	}

	private void initWidget() {
		adapter = new AdapterIndiaNews(mContext);
		vf = (ViewFlipper) root.findViewById(R.id.vf);
		LayoutInflater inflater = LayoutInflater.from(mContext);
		mLoadMoreFooter = inflater.inflate(R.layout.child_loading_screen, null);
		home_list_item = (PullToRefreshListView) root
				.findViewById(R.id.home_list_item);

		home_list_item.setOnScrollListener(new OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

				int lastInScreen = firstVisibleItem + visibleItemCount;
				if ((lastInScreen == totalItemCount) && is_loop == false
						&& page < totalPage && is_refresh == false) {
					mLoadMoreFooter.setVisibility(View.VISIBLE);
					home_list_item.addFooterView(mLoadMoreFooter);
					is_loop = true;
					page++;
					totalPage--;
					getDataService(page);
				}

			}
		});

		home_list_item.setOnRefreshListener(new OnRefreshListener() {
					@Override
					public void onRefresh() {
						refresh();
					}
				});

	}

	private void refresh() {
		arrayCategory = new ArrayList<ObjectCategory>();
		home_list_item.onRefreshComplete();
		getDataService(DEFAULT);
	}

	private class getDBFavouriteTask extends
			AsyncTask<Void, Void, ArrayList<ObjectCategory>> {

		@Override
		protected ArrayList<ObjectCategory> doInBackground(Void... url) {
			ArrayList<ObjectCategory> listCategory = new ArrayList<ObjectCategory>();
			ObjectCategory objectCategory = new ObjectCategory();
			ArrayList<ObjectNews> listNews = new ArrayList<ObjectNews>();
			Cursor cursorNews = DBHandler.getNewsByFavourite(mContext);
			if(cursorNews.moveToFirst())
			{
				do{
					ObjectNews objectNews = new ObjectNews();
					objectNews.setNews_id_category(cursorNews.getString(1));
					objectNews.setNews_link(cursorNews.getString(2));
					objectNews.setNews_imageSRC(cursorNews.getString(3));
					objectNews.setNews_description(cursorNews.getString(4));
					objectNews.setNews_pubDate(cursorNews.getString(5));
					objectNews.setNews_favourite(cursorNews.getString(6));
					objectNews.setNews_name_category(mContext.getResources().getString(R.string.favourite));
					listNews.add(objectNews);
					
				}while(cursorNews.moveToNext());
			}
			objectCategory.setCategory_title(mContext.getResources().getString(R.string.favourite));
			objectCategory.setCategory_listNews(listNews);
			listCategory.add(objectCategory);
			return listCategory;
		}

		@Override
		protected void onPostExecute(ArrayList<ObjectCategory> listCategory) {
			if (listCategory.size() > 0) {
				adapter.setData(listCategory);
				home_list_item.setAdapter(adapter);
				vf.setDisplayedChild(HOME);
			} else {
				vf.setDisplayedChild(ERRO);
			}
		}
	}

}
